/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;

import com.tech15.core.model.SysUser;
import com.tech15.core.vo.query.SysUserQuery;

@Repository
public class SysUserDao extends BaseHibernateDao<SysUser,Long>{

	public Class getEntityClass() {
		return SysUser.class;
	}
	
	public Page findPage(SysUserQuery query) {
		String sql = "select t.*,t1.deptname as orgInfo from sys_user t left join sys_dept t1 on t.deptid=t1.deptid where isadmin=0 "
			+ "/~ and t1.deptname like '%[orgInfo]%' ~/"
		  	+ "/~ and t.username like '%[username]%' ~/"
		  	+ "/~ and t.realname like '%[realname]%' ~/"
		  	+ "/~ and t.state = '[state]' ~/"
		  	+ "/~ and t.category = [category] ~/"
		  	+ "/~ and t.userrole = [userrole] ~/"
			+ "/~ order by [sortColumns] ~/";

		Map map = new HashMap();
		map.put("userid", "Long");
		map.put("username", "String");
		map.put("realname", "String");
		map.put("memo", "String");
		map.put("state", "String");
		map.put("isadmin", "Integer");
		map.put("category", "Integer");
		map.put("orgInfo", "String");
		map.put("deptname", "String");
		return pageSqlQuery(sql,query,map,SysUser.class);
	}

	
	public List<SysUser> selectUser(Long keyid,String realname){
		String sql = "select t.username,t.userid as id,t.realname as name,t.realname,t1.deptname as orgInfo from sys_user t" +
				" left join sys_dept t1 on t.deptid=t1.deptid where t.state=1 and t.isadmin=0 ";
		if(isNotEmpty(keyid)){
			sql += " and t.deptid="+keyid;
		}
		if(isNotEmpty(realname)){
			sql += " and t.realname like '%"+realname+"%'";
		}
		sql += " order by userrole,nlssort(t.realname,'NLS_SORT=SCHINESE_PINYIN_M')";
		Map map = new HashMap();
		map.put("username", "String");
		map.put("realname", "String");
		map.put("id","Long");
		map.put("name","String");
		map.put("orgInfo","String");
		return findSqlAll(sql, map, SysUser.class);
	}

	public List<SysUser> findByDept(Long deptid) {
		String sql = "from SysUser t where t.deptid=?";
		return getHibernateTemplate().find(sql,deptid);
		
	}

	public List<SysUser> selectOnlyUser(String realname) {
		String sql = "select t.userid,t.userid as id,t.realname as name,t.realname from sys_user t where t.isadmin=0 ";
		if(isNotEmpty(realname)){
			sql += " and t.realname like '%"+realname+"%'";
		}
		sql += " order by userrole,nlssort(t.realname,'NLS_SORT=SCHINESE_PINYIN_M')";
		Map map = new HashMap();
		map.put("userid", "Long");
		map.put("realname", "String");
		map.put("id","Long");
		map.put("name","String");
		return findSqlAll(sql, map, SysUser.class);
	}
	
	public SysUser getByUsername(java.lang.String v) {
		return (SysUser) findByProperty("username",v);
	}

	public List<SysUser> searchLeader(Long keyid, String realname, Integer orgType) {
		String sql = "select t.username,t.userid as id,t.realname as name,t.realname,t1.deptname as orgInfo from sys_user t" +
		" left join sys_dept t1 on t.deptid=t1.deptid where t.state=1 and t.isadmin=0 " +
		" and (t.category = 2 or t.category=1) ";
		if(isNotEmpty(keyid)){
			sql += " and t.deptid="+keyid;
		}
		if(isNotEmpty(realname)){
			sql += " and t.realname like '%"+realname+"%'";
		}
		/*if(isNotEmpty(keyid)){
			sql += " and (t.category = 2 or t.category=1)";
		}*/
		// and (t.userrole = 1 or t.userrole=2 or t.userrole=3 or t.userrole=4 or t.userrole=5 or t.userrole=6)
		sql += " order by userrole,nlssort(t.realname,'NLS_SORT=SCHINESE_PINYIN_M')";
		Map map = new HashMap();
		map.put("username", "String");
		map.put("realname", "String");
		map.put("id","Long");
		map.put("name","String");
		map.put("orgInfo","String");
		return findSqlAll(sql, map, SysUser.class);
	}	

}
